package com.igeekhome.controller.api;

import com.igeekhome.pojo.DNetwork;
import com.igeekhome.service.INetworkService;
import com.igeekhome.util.DtreeLayUINode;
import com.igeekhome.util.ResultBean;
import com.igeekhome.util.TableResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Romeo
 */
@RestController
@RequestMapping("/api/network")
public class NetworkControllerApi {
    @Autowired
    private INetworkService networkService;

    @RequestMapping("/treeList")
    public ResultBean treeList(HttpServletRequest request){
        // 通过Session来出当前所在的网点信息
        DNetwork network = (DNetwork) request.getSession().getAttribute("network");
        Integer nid = network.getNetworkId();
        DtreeLayUINode dtreeLayUINode = networkService.selectByIdToTree(nid);
        List<DtreeLayUINode> list = new ArrayList<>();
        list.add(dtreeLayUINode);
        return ResultBean.ok(list);
    }

    @RequestMapping("/tableList")
    public TableResultBean tableList(HttpServletRequest request, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit){
        // 通过Session来出当前所在的网点信息
        DNetwork network = (DNetwork) request.getSession().getAttribute("network");
        Integer nid = network.getNetworkId();
        return networkService.selectByIdToTable(nid,page,limit);
    }
    @RequestMapping("/add")
    public ResultBean add(DNetwork dNetwork, String province, String city, String area, String address){
        return networkService.add(dNetwork,province,city,area,address);
    }
    @RequestMapping("/edit")
    public ResultBean edit(DNetwork dNetwork, String province, String city, String area, String address){
        return networkService.edit(dNetwork,province,city,area,address);
    }
    @RequestMapping("/delete")
    public ResultBean logicDelete(Integer nid){
        System.out.println("delete====>"+nid);
        return networkService.logicDelete(nid);
    }



}
